<?php
require_once '../model/DBUtil.class.php';
$dbutil = new DBUtil("172.16.120.99", "yxy", "root", "123456");


$pageNo = isset($_GET["pageNo"]) ? $_GET["pageNo"] : 1;
$pageSize = isset($_GET["pageSize"]) ? $_GET["pageSize"] : 10;
$sql = "select e.empid,e.empName,e.age,e.gender,e.phone,e.email,d.deptName from tb_dept d,tb_employee e where d.deptid=e.deptid";
$page = $dbutil->queryByPage($sql, $pageNo, $pageSize);

header("Content-Type:text/html;charset=utf-8");


//总页数   
$pageCount = $page["total"]%10==0 ? $page["total"]/10 : (int)($page["total"]/10)+1;
?>
<!DOCTYPE html>
<html>
    <head>
        <title>员工信息列表</title>
        <link type="text/css" rel="stylesheet" href="../bootstrap/css/bootstrap.min.css" />
        <script type="text/javascript" src="../bootstrap/js/jquery-1.9.1.min.js"></script>
        <script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script>
        <script type="text/javascript">
        /**
         * changeTo表示翻到第几页，changeTo为0时表示翻下一页，changeTo为-1时表示翻上一页，changeTo大于0时表示翻到指定页码
         */
        function changePage(changeTo) {
			var url = "index.php";
			var pageNo = "<?php echo $pageNo;?>";
			var pageCount = "<?php echo $pageCount;?>";
			if(changeTo <= -1){
			    //上一页
				if(pageNo == 1){
			    	alert("对不起，当前展示的已经是第一页了！");
			        return;
				}
				changeTo = pageNo - 1;
			}else if(changeTo == 0){
			    //下一页
			    if(pageNo == pageCount){
			        alert("对不起，当前展示的已经是最后一页了！");
			        return;
				}
				changeTo = parseInt(pageNo) + 1;
			}else{
			    if(changeTo > pageCount){
			    	alert("对不起，当前数据共"+pageCount+"页，不能跳转到第"+changeTo+"页！");
			        return;
				}
			}
			url += "?pageNo=" + changeTo;
			location.href = url;
		}
        </script>
    </head>
    <body>
        <table class="table table-bordered table-striped table-hover">
            <thead>
                <tr>
                    <th>编号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>电话</th>
                    <th>邮箱</th>
                    <th>所属部门</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                foreach ($page["rows"] as $emp){
                    echo "<tr>";
                    echo "<td>{$emp['empid']}</td>";
                    echo "<td>{$emp['empName']}</td>";
                    echo "<td>{$emp['age']}</td>";
                    echo "<td>".($emp['gender']==1?'男':'女')."</td>";
                    echo "<td>{$emp['phone']}</td>";
                    echo "<td>{$emp['email']}</td>";
                    echo "<td>{$emp['deptName']}</td>";
                    echo "</tr>";
                }
                ?>
            </tbody>
        </table>
        <nav aria-label="Page navigation" class="text-center">
            <ul class="pagination">
                <li>
                  <a href="javascript: changePage(-1);" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                  </a>
                </li>
                <li><a href="javascript: changePage(1);">1</a></li>
                <li><a href="javascript: changePage(2);">2</a></li>
                <li><a href="javascript: changePage(3);">3</a></li>
                <li><a href="javascript: changePage(4);">4</a></li>
                <li><a href="javascript: changePage(5);">5</a></li>
                <li>
                  <a href="javascript: changePage(0);" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                  </a>
                </li>
            </ul>
        </nav>
    </body>
</html>